package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.AdminEditRole;
import ch.fhnw.qualiscan.application.modals.AdminNewRole;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.helpers.adminRoleTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Role;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

/**
 * Edit roles
 *
 */
@SuppressWarnings("serial")
public class AdminTab6 extends VerticalLayout{

	public Admin admin;
	private Table roleTable;
	public BeanItemContainer<adminRoleTableArgs> tableContainer;
	private Language lang;
	
	public AdminTab6(Admin admin) {
		this.admin = admin;
		lang = admin.userSession.getLanguage();

		buildLayout();
	}
	
	public void buildLayout() {
		removeAllComponents();
		addComponent(addDomainForm());
		
		VerticalLayout tableWrap = new VerticalLayout();
		tableWrap.addComponent(buildTable());
		tableWrap.setMargin(true);
		addComponent(tableWrap);
	}
	
	public Table buildTable() {
		tableContainer = new BeanItemContainer<adminRoleTableArgs>(adminRoleTableArgs.class);
		
		List<Role> roleList = admin.adminModel.getRoles();
		for(int i = 0; i < roleList.size(); i++) {
			adminRoleTableArgs arta = new adminRoleTableArgs();
			arta.setRole(roleList.get(i));
			arta.setPermission(roleList.get(i).getPermissions().getTitle());
			arta.setTitleEng(admin.adminModel.getRoleText(roleList.get(i), lang).getTitle());
			
			tableContainer.addBean(arta);
		}
		
		roleTable = new Table();
		roleTable.setWidth("100%");
		roleTable.setPageLength(0);
		roleTable.setSelectable(true);
		
		roleTable.addGeneratedColumn("roleEditButton", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
			
 				Button editButton = new Button(TM.get("action.edit"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						adminRoleTableArgs arta = (adminRoleTableArgs) event.getButton().getData();
						AdminTab6 caller = (AdminTab6) event.getButton().getParent().getParent().getParent();
						admin.openModal(new AdminEditRole(arta, caller));
					}
				});
 				editButton.setData(itemId);
				editButton.setIcon(Config.ICON_EDIT);
				return editButton;
			}
		});
		
		
		roleTable.setContainerDataSource(tableContainer);
		
		roleTable.setColumnHeader("titleEng", TM.get("admin.roletable.engtitle"));
		roleTable.setColumnHeader("permission", TM.get("admin.roletable.permission"));
		
		roleTable.setVisibleColumns(new Object[]{"titleEng", "permission", "roleEditButton"});
		
		return roleTable;
	}
	
	public HorizontalLayout addDomainForm() {
		HorizontalLayout addDomainForm = new HorizontalLayout();
		addDomainForm.setMargin(true, true, false, true);
		
		Button addButton =  new Button(TM.get("action.add"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminTab6 caller = (AdminTab6) event.getButton().getParent().getParent();
				admin.openModal(new AdminNewRole(tableContainer, caller));
			}
		});
		addButton.setIcon(Config.ICON_ADD);
		addDomainForm.addComponent(addButton);
		
		return addDomainForm;
	}
}
